package com.example.infrastructure.impl;

import java.nio.file.Path;

import org.springframework.stereotype.Component;

import com.alibaba.dashscope.audio.asr.recognition.Recognition;
import com.alibaba.dashscope.audio.asr.recognition.RecognitionParam;
import com.example.infrastructure.TtsService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

@Component
public class BaiLianAIService extends TtsService {

	public static RecognitionParam param = RecognitionParam.builder().model("paraformer-realtime-v2").format("wav")
			.sampleRate(16000).apiKey("sk-9ed83dc9aed5466a9efa93396b2d4ea5").build();

	@Override
	public String llmResolveText(Path filePath) {
		String content = "";

		Recognition recognizer = new Recognition();
		String result = recognizer.call(param, filePath.toFile());

		Gson gson = new GsonBuilder().setPrettyPrinting().create();
		JsonObject jsonObject = gson.fromJson(result, JsonObject.class);

		if (jsonObject.has("sentences")) {
			for (JsonElement sent : jsonObject.get("sentences").getAsJsonArray()) {
				JsonObject sentObj = sent.getAsJsonObject();
				System.out.println(sentObj.get("text").getAsString());
				content += sentObj.get("text").getAsString();
			}
		}

		return content;
	}
}
